package com.ktc.base.controller;

import com.ktc.base.entity.Label;
import com.ktc.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.net.InetAddress;
import java.util.List;
import java.util.Map;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
@RestController
@RequestMapping("/label")
@CrossOrigin
public class LabelController {

    @Autowired
    private LabelService labelService;


    /**
     * 根据id查询用户
     *
     * @param labelid
     * @return
     */
    @GetMapping("/{labelid}")
    public Result findById(@PathVariable String labelid) {

        Label label = labelService.findById(labelid);

        return new Result(true, StatusCode.OK, "查询成功", label);
    }

    /**
     * 新增
     *
     * @param label
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Label label) {

        labelService.save(label);

        return new Result(true, StatusCode.OK, "新增成功");
    }

    /**
     * 删除
     *
     * @param labelid
     * @return
     */
    @DeleteMapping("/{labelid}")
    public Result delete(@PathVariable String labelid) {

        labelService.delete(labelid);

        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 修改
     *
     * @param labelid
     * @param label
     * @return
     */
    @PutMapping("/{labelid}")
    public Result update(@PathVariable String labelid, @RequestBody Label label) {

        label.setId(labelid);
        labelService.update(label);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    public Result findAll() {

        int i=1/0;
        List<Label> labelList = labelService.findAll();

        return new Result(true, StatusCode.OK, "查询成功", labelList);
    }

    /**
     * 条件搜索标签
     * @param searchMap
     * @return
     */
    @PostMapping("/search")
    public Result search(@RequestBody Map searchMap) {

        List<Label> labelList=labelService.search(searchMap);

        return new Result(true, StatusCode.OK, "查询成功", labelList);
    }


    /**
     * 条件分页查询
     * @param searchMap
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    public Result findPage(@RequestBody Map searchMap, @PathVariable Integer page, @PathVariable Integer size) {


        Page<Label> pageData=labelService.findPage(searchMap,page,size);

        return new Result(true, StatusCode.OK, "查询成功",
                new PageResult<>(pageData.getTotalElements(),pageData.getContent()));
    }
}
